package com.gui.components.search;

import java.awt.ComponentOrientation;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import com.dataModel.Quran;
import com.dataModel.Sourah;
import com.dataModel.descriptors.AyahDescriptor;
import com.panels.MainPanel;
import com.utils.search.SearchResults;

public class SearchResultsTable extends JTable implements KeyListener,MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3092733562518606622L;
	private final static int SOURAH_NAME_COLUMN_INDEX=0;
	private final static int AYAH_NUMBER_COLUMN_INDEX=1;
	private final static int AYAH_TEXT_COLUMN_INDEX=2;
	public SearchResultsTable()
	{
		super(10,3);
		this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		this.setModel(new SearchResultsTableModel(10,3));
		this.setRowHeight(22);
		this.addMouseListener(this);
		TableCellRenderer tableCellRenderer = new SearchTabelCellRenderer();
		for (int i = 0; i < this.getColumnCount(); i++) {
			this.getColumnModel().getColumn(i).setCellRenderer(
					tableCellRenderer);
		}

		
		initGui();
	}
	
	private void initGui()
	{
		this.getColumnModel().getColumn(SOURAH_NAME_COLUMN_INDEX).setHeaderValue("Sourah");
		this.getColumnModel().getColumn(AYAH_NUMBER_COLUMN_INDEX).setHeaderValue("Ayah Number");
		this.getColumnModel().getColumn(AYAH_TEXT_COLUMN_INDEX).setHeaderValue("Ayah");
		//-------------------------------------------------------------------
		this.getColumnModel().getColumn(SOURAH_NAME_COLUMN_INDEX).setPreferredWidth(100);
		this.getColumnModel().getColumn(AYAH_NUMBER_COLUMN_INDEX).setPreferredWidth(100);
		this.getColumnModel().getColumn(AYAH_TEXT_COLUMN_INDEX).setPreferredWidth(600);
		//---------------------------------------------------------------------
	}
	public void setSearchResults(SearchResults searchResults)
	{
		clearTable();
		List<AyahDescriptor> ayatDescriptors=searchResults.getMatchedAyat();
		
		
		for(AyahDescriptor ayahDescriptor:ayatDescriptors)
		{
		Sourah sourah=Quran.getSowar()[ayahDescriptor.getSourahNumber()-1];
		String AyahNumber=ayahDescriptor.getAyahNumber()+"";
		String AyahText=Quran.getSowar()[ayahDescriptor.getSourahNumber()-1].
		getAyat().get(ayahDescriptor.getAyahNumber()-1).getTextMoshakal();
		Object []row={sourah,AyahNumber,AyahText};
		((DefaultTableModel)this.getModel()).addRow(row);
		}
	}
	
	void clearTable()
	{
	for(int i=this.getModel().getRowCount()-1;i>=0;i--)
	{
		((DefaultTableModel)this.getModel()).removeRow(i);
	}
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()%2==0)
		{
			int rowIndex = this.rowAtPoint(e.getPoint());
            Sourah sourah=(Sourah)this.getModel().getValueAt(rowIndex,
					SOURAH_NAME_COLUMN_INDEX);
			int sourahNumber=sourah.getNumber();
			int ayahNumber=Integer.parseInt(this.getModel().getValueAt(rowIndex,
					AYAH_NUMBER_COLUMN_INDEX).toString());
			int pageNumber=Quran.getPageNumber(sourahNumber, ayahNumber);
			MainPanel.getMainPanel().getPagesPanel().displayPage(pageNumber);
		}
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
